/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DataAccess;

import Entities.CustomConverter;
import Entities.EnrollEventEntities;
import Entities.EventEntities;
import Entities.RegisterEntities;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author ducdm_b00875
 */
public class RegisterDataAccess {
    
    ConnectDatabase objCD = new ConnectDatabase();
    CustomConverter convert = new CustomConverter();
    List<RegisterEntities> regisetList;
    RegisterEntities registerEntities;
    private List<EventEntities> eventList;
    EventEntities objEvent;

    
    public RegisterDataAccess()
    {
        regisetList = new ArrayList<RegisterEntities>();
        eventList = new ArrayList<EventEntities>();
    }
    
    public List<RegisterEntities> getRegisterById(String userId)throws Exception, ClassNotFoundException
    {
        regisetList.clear();
        objCD.open();
        objCD.setMode(ConnectDatabase.CALLABLE);
        objCD.setStatement("{call sp_Register_ViewById(?)}");
        objCD.castm.setString(1, userId);
        objCD.doQuery();
        if (objCD.rs != null) {
            while (objCD.rs.next()) {                
                registerEntities = new RegisterEntities(objCD.rs.getInt("RegisterId"), objCD.rs.getString("RegisterName"), objCD.rs.getString("Email"), objCD.rs.getString("UserName"), objCD.rs.getString("Password"), objCD.rs.getFloat("MoneyNumber"), objCD.rs.getString("Phone"), objCD.rs.getString("Gender"), objCD.rs.getString("Images"), objCD.rs.getString("Address"), objCD.rs.getDate("DateCreate"));
                regisetList.add(registerEntities);
            }
        }
        objCD.close();
        return regisetList;
    }
    
    public boolean checkLogin(RegisterEntities registerEntities)throws Exception,ClassNotFoundException{
        
        regisetList.clear();
        objCD.open();
        objCD.setMode(ConnectDatabase.PREPARED);
        objCD.setStatement("Select UserName, [Password] from [Register]  where UserName = '"+registerEntities.getUserName()+"' and [Password] = '"+registerEntities.getPassword()+"'");
        objCD.doQuery();
        if (objCD.rs != null) {
            while (objCD.rs.next()) { 
                return true;
            }
        }
        objCD.close();
        return false;
    }
    
    public List<EventEntities> getEventByUser(String CatId) throws Exception, ClassNotFoundException{
        eventList.clear();
        objCD.open();
        objCD.setMode(ConnectDatabase.CALLABLE);
        objCD.setStatement("{call sp_Register_ViewEventByUser(?)}");
        objCD.castm.setString(1, "1");
        objCD.doQuery();
        if (objCD.rs != null) {
            while (objCD.rs.next()) {                
                objEvent = new EventEntities(objCD.rs.getInt("EventID"), objCD.rs.getString("EventName"), objCD.rs.getDate("StartDate"), objCD.rs.getDate("EndDate"), objCD.rs.getString("Address"), objCD.rs.getString("Images"), objCD.rs.getString("Description"), objCD.rs.getFloat("Price"), objCD.rs.getString("NameWin"), objCD.rs.getInt("CountView"));
                eventList.add(objEvent);
            }
        }
        objCD.close();
        return eventList;
    }
    
    public boolean registerEvent(String eventId, String RegisterId, String AdherenceType)throws Exception, ClassNotFoundException{
            int check = 0;
            objCD.open();
            objCD.setMode(ConnectDatabase.CALLABLE);
            objCD.setStatement("{call sp_EnrollEvent_Insert(?,?,?,?)}");
            objCD.castm.setString(1,eventId);
            objCD.castm.setString(2,RegisterId);
            objCD.castm.setString(3,AdherenceType);
            objCD.castm.setFloat(4,100);
            
            check = objCD.doUpdate();
            if (check == 0) {
                return false;
            }
            objCD.close();
            return true;
    }
    
    public boolean insertRegister(RegisterEntities registerEntities)throws Exception, ClassNotFoundException{
            int check = 0;
            objCD.open();
            objCD.setMode(ConnectDatabase.CALLABLE);
            objCD.setStatement("{call sp_Register_Insert(?,?,?,?,?,?,?,?,?)}");
            objCD.castm.setString(1,registerEntities.getRegisterName());
            objCD.castm.setString(2,registerEntities.getEmail());
            objCD.castm.setString(3,registerEntities.getUserName());
            objCD.castm.setString(4,registerEntities.getPassword());
            objCD.castm.setFloat(5,registerEntities.getMoneyNumber());
            objCD.castm.setString(6,registerEntities.getPhone());
            objCD.castm.setString(7,registerEntities.getGender());
            objCD.castm.setString(8,"123");
            objCD.castm.setString(9,registerEntities.getAddress());
            
            check = objCD.doUpdate();
            if (check == 0) {
                return false;
            }
            objCD.close();
            return true;
    }
    
    public boolean updateRegister(RegisterEntities registerEntities)throws Exception, ClassNotFoundException{
            int check = 0;
            objCD.open();
            objCD.setMode(ConnectDatabase.CALLABLE);
            objCD.setStatement("{call sp_Register_Update(?,?,?,?,?,?,?,?)}");
            objCD.castm.setInt(1,registerEntities.getRegisterId());
            objCD.castm.setString(2,registerEntities.getRegisterName());
            objCD.castm.setString(3,registerEntities.getEmail());
            objCD.castm.setString(4,registerEntities.getUserName());
            objCD.castm.setString(5,registerEntities.getPhone());
            objCD.castm.setString(6,registerEntities.getGender());
            objCD.castm.setString(7,registerEntities.getImages());
            objCD.castm.setString(8,registerEntities.getAddress());
            
            check = objCD.doUpdate();
            if (check == 0) {
                return false;
            }
            objCD.close();
            return true;
    }
    
    public List<RegisterEntities> deleteRegister()throws Exception, ClassNotFoundException
    {
        regisetList.clear();
        objCD.open();
        objCD.setMode(ConnectDatabase.CALLABLE);
        objCD.setStatement("{call sp_Register_ViewById(?)}");
        objCD.castm.setInt(1, 1);
        objCD.doQuery();
        if (objCD.rs != null) {
            while (objCD.rs.next()) {                
                registerEntities = new RegisterEntities(objCD.rs.getInt("RegisterId"), objCD.rs.getString("RegisterName"), objCD.rs.getString("Email"), objCD.rs.getString("UserName"), objCD.rs.getString("Password"), objCD.rs.getFloat("MoneyNumber"), objCD.rs.getString("Phone"), objCD.rs.getString("Gender"), objCD.rs.getString("Images"), objCD.rs.getString("[Address]"), objCD.rs.getDate("DateCreate"));
                regisetList.add(registerEntities);
            }
        }
        objCD.close();
        return regisetList;
    }
    
    public List<RegisterEntities> getRegisetList() {
        return regisetList;
    }

    public void setRegisetList(List<RegisterEntities> regisetList) {
        this.regisetList = regisetList;
    }
}
